package a09_贪心算法;

/**
 * <p>
 * a04_最大子序和复习0
 * </p>
 *
 * @author flyduck
 * @since 2025/1/15
 */
public class a04_最大子序和复习0 {

    public static void main(String[] args) {
        int i = maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});
    }
    //-2,1,-3,4,-1,2,1,-5,4
    public static int maxSubArray(int[] nums) {
        int sum = 0;
        int maxSum = Integer.MIN_VALUE;
        int idx = 0;
        while (idx < nums.length) {
            sum = sum + nums[idx];
            maxSum = Math.max(maxSum, sum);
            if(sum < 0){
                sum = 0;
            }
            idx++;
        }
        return maxSum;
    }
}
